﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class Item : MonoBehaviour, IDragHandler, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler
{
    /// <summary>
    /// 判断是否里面拥有格子
    /// </summary>
    public bool isUsed = false;

    public string _name = "";

    public int num = 0;

    Inventory _inventory;
    void Start()
    {
        
    }

    public void GetItem(Inventory inventory)
    {
        _inventory = inventory;
        isUsed = true;
        num += 1;
        this.transform.GetChild(1).GetComponent<Text>().text = num.ToString();

        this.transform.GetChild(0).GetComponent<Image>().sprite = Resources.Load(inventory.sprite, typeof(Sprite)) as Sprite;
        this.transform.GetChild(0).gameObject.SetActive(true);

        _name = inventory.name;
    }

    public void Substration()
    {
        num -= 1;
        if (num == 0)
        {
            Destroy();
        }
    }
    public void Destroy()
    {
        this.transform.GetChild(0).GetComponent<Image>().sprite = null;
        this.transform.GetChild(0).gameObject.SetActive(false);
        this.transform.GetChild(1).GetComponent<Text>().text = null;

        isUsed = false;
        num = 0;
        _name = null;
    }
    public void OnDrag(PointerEventData eventData)
    {

    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if (isUsed)
        {
            ItemTools.Create(_inventory, this.GetComponent<Item>());
        }
    }

    public void OnPointerEnter(PointerEventData eventData)
    {

    }

    public void OnPointerExit(PointerEventData eventData)
    {

    }
}
